"""认证相关"""

from django.db.models import Q
from django.shortcuts import (
    render, redirect
)

from sales import models
from sales.my_forms import RegisterForm
from sales.utils.hashlib_func import set_md5


def register(request):
    """
    注册功能
    :param request:
    :return:
    """
    if request.method == 'GET':
        register_form_obj = RegisterForm()
        return render(request, 'register.html', {'register_form_obj': register_form_obj})
    else:
        register_form_obj = RegisterForm(request.POST)
        if register_form_obj.is_valid():
            print(register_form_obj.cleaned_data)
            # 对密码进行加密
            telephone = register_form_obj.cleaned_data.get('telephone')
            username = register_form_obj.cleaned_data.get('username')
            if models.UserInfo.objects.filter(Q(username=username) | Q(telephone=telephone)):
                return render(request, 'register.html',
                              {'register_form_obj': register_form_obj, 'errors': '用户已存在或手机号已注册'})
            register_form_obj.cleaned_data.pop('r_password')
            password = register_form_obj.cleaned_data.pop('password')
            password = set_md5(password)
            register_form_obj.cleaned_data.update({'password': password})
            models.UserInfo.objects.create(
                **register_form_obj.cleaned_data
            )
            return redirect('login')
        else:
            return render(request, 'register.html', {'register_form_obj': register_form_obj})


def login(request):
    """
    登录功能
    :param request:
    :return:
    """
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        username = request.POST.get('username')
        password = request.POST.get('password')
        user_obj = models.UserInfo.objects.filter(username=username, password=set_md5(password)).first()
        if user_obj:
            # 将用户信息保存到session中
            request.session['user_id'] = user_obj.id
            return redirect('customers')
        else:
            # return redirect('login')
            return render(request, 'login.html', {'error': '用户名或密码错误'})


def logout(request):
    request.session.delete('user_id')
    return redirect('login')